// pages/game/game.js
var data = require('../../utils/data.js')

var map =[
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0]
]

var box = [
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0]
]

var w = 40
var row = 0
var col = 0

Page({

  /**
   * 页面的初始数据
   */
  data: {
    level:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let level = options.level
    this.setData({
      level: parseInt(level) + 1
    })
    let that = this
    // wx.createSelectorQuery()
    // .select('myCanvas') // 在 WXML 中填入的 id
    // .fields({ node: true, size: true })
    // .exec((res) => {
    //     // Canvas 对象
    //     that.canvas = res[0].node,
    //     // 渲染上下文
    //     that.ctx = canvas.getContext('2d')
    // })

    //t
    this.ctx = wx.createCanvasContext('myCanvas')
    this.initMap(level)
    this.drawCanvas()
  },

  initMap:function(level){
    let mapdata = data.maps[level]
    for(var i = 0 ; i < 8 ; i++){
      for(var j = 0; j < 8; j++){
        box[i][j] = 0
        map[i][j] = mapdata[i][j]

        if(mapdata[i][j] == 4){
          box[i][j] = 4
          map[i][j] = 2

        }else if(mapdata[i][j] == 5){
          map[i][j] = 2
          row = i
          col = j
        }
      }
    }
   
  },
  drawCanvas:function(){
    let ctx = this.ctx
    ctx.clearRect(0,0,320,320)

    for(var i = 0; i < 8; i++){
      for(var j = 0; j < 8; j++){
        let img = 'ice'
        if(map[i][j] == 1){
          img = 'stone'
        }else if(map[i][j] == 3){
          img = 'pig'
        }

        ctx.drawImage('/images/icons/'+img+'.png', j*w,i*w,w,w)

        if(box[i][j] == 4){
          ctx.drawImage('/images/icons/box.png',j*w,i*w,w,w)
        }
      }
    }
    ctx.drawImage('/images/icons/bird.png',col*w,row*w,w,w)
    ctx.draw()
  },

  move:function(e){
    let dir = e.currentTarget.dataset.d;
    //console.log(dir)
    dir = parseInt(dir);
    //console.log(dir)
    let dmcol = [0,-1,0,1]
    let dmrow = [-1,0,1,0]
    if((dir == 1 && row > 0)||(dir == 2 && col > 0)||(dir == 3 && row <7)||(dir == 4 && col < 7)){
      let trow = row+dmrow[dir - 1]
      let tcol = col+dmcol[dir - 1]
      let ttr = trow+dmrow[dir - 1]
      let ttc = tcol+dmcol[dir - 1]
      if(map[trow][tcol] != 1&&box[trow][tcol] != 4){
        row = trow
        col = tcol
      }
      else if(box[trow][tcol] == 4){
        if((dir == 1 && trow > 0)||(dir == 2 && tcol > 0)||(dir == 3 && trow <7)||(dir == 4 && tcol < 7))
        {
          if(map[ttr][ttc] != 1 && box[ttr][ttc] != 4){
            box[ttr][ttc] = 4
            box[trow][tcol] = 0
            row = trow
            col = tcol
          }
        }
      }
    }
    this.drawCanvas()
    this.checkWin()
  },
  restart:function(){
    this.initMap(this.data.level - 1)
    this.drawCanvas()
  },
  isWin:function(){
    for(var i = 0; i < 8; i++){
      for(var j = 0; j < 8; j++){
        if(box[i][j] == 4 && map[i][j] != 3){
          return false
        }
      }
    }
    return true
  },
  checkWin:function(){
    if(this.isWin()){
      wx.showModal({
        title:"恭喜",
        content:'游戏成功',
        showCancel:false
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})